//
//  LXSaveTableViewCell.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/30.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LXSaveTableViewCell.h"

@implementation LXSaveTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}
-(void)createSubViews{
    self.userImage = [[UIImageView alloc]init];
    self.userImage.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.userImage];
    [self.userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(10);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.equalTo(self.userImage.mas_right).offset(10);
        make.right.mas_equalTo(-60);
        make.height.mas_equalTo(20);
    }];
    
    self.lastUptrackTitleLabel = [[UILabel alloc]init];
    self.lastUptrackTitleLabel.textColor = [UIColor lightGrayColor];
    self.lastUptrackTitleLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.lastUptrackTitleLabel];
    [self.lastUptrackTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(5);
        make.left.equalTo(self.titleLabel);
        make.height.mas_equalTo(15);
        make.right.equalTo(self.titleLabel);
    }];
    
    self.otherLabel = [[UILabel alloc]init];
    self.otherLabel.font = [UIFont systemFontOfSize:13];
    self.otherLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.otherLabel];
    [self.otherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lastUptrackTitleLabel.mas_bottom).offset(5);
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.titleLabel);
        make.height.mas_equalTo(15);
    }];
    
    self.saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.saveBtn];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(25);
        make.width.mas_equalTo(30);
        make.right.mas_equalTo(-20);
    }];
}
-(void)setFrom:(NSString *)from{
    self.FromStr = from;
    if ([from isEqualToString:@"delete"]) {
         [self.saveBtn setBackgroundImage:[UIImage imageNamed:@"cell_delete_n@2x.png"] forState:UIControlStateNormal];
        [self.saveBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        [self.saveBtn setBackgroundImage:[UIImage imageNamed:@"find_album_unfav_n@2x.png"] forState:UIControlStateNormal];
        [self.saveBtn addTarget:self action:@selector(saveBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
}
-(void)setCellModel:(LXSaveModel *)cellModel{
    self.bringModel = cellModel;
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:cellModel.coverMiddle] placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
    self.titleLabel.text = cellModel.title;
    self.lastUptrackTitleLabel.text = cellModel.lastUptrackTitle;
    if ([self.FromStr isEqualToString:@"delete"]) {
        NSString *date = [LXTimeTool getDateWithUpdatedAt:[cellModel.lastUptrackAt integerValue]];
        self.otherLabel.text = [NSString stringWithFormat:@"更新%@",date];
    }else{
    NSString *track =[NSString stringWithFormat:@"%ld集",cellModel.tracks];
    NSTextAttachment *textAttachment01 = [[NSTextAttachment alloc] init];
    textAttachment01.image = [UIImage imageNamed: @"album_tracks@2x.png"];  //设置图片源
    textAttachment01.bounds = CGRectMake(0, -1, 13, 13);          //设置图片位置和大小
    NSMutableAttributedString *attrStr01 = [[NSMutableAttributedString alloc] initWithString:track];
    
    [attrStr01 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 13] range: NSMakeRange(0,track.length)];
    NSAttributedString *attrStr11 = [NSAttributedString attributedStringWithAttachment: textAttachment01];
    
    [attrStr01 insertAttributedString: attrStr11 atIndex: 0];  //NSTextAttachment占用一个字符长度，插入后原字符串长度增加1
    self.otherLabel.attributedText = attrStr01;
    }
}
-(void)setIndex:(NSInteger)index{
    self.deleteIndex = index;
}
-(void)setSection:(NSInteger)section{
    self.deleteSection = section;
}
-(void)saveBtnAction:(UIButton*)btn{
    if (btn.selected == NO) {
        [btn setBackgroundImage:[UIImage imageNamed:@"find_album_fav_n@2x.png"] forState:UIControlStateNormal];
        [self.saveDelegate bringSaveOrDelete:self.bringModel type:@"save" index:self.deleteIndex section:self.deleteSection];
    }else{
        [btn setBackgroundImage:[UIImage imageNamed:@"find_album_unfav_n@2x.png"] forState:UIControlStateNormal];
        [self.saveDelegate bringSaveOrDelete:self.bringModel type:@"delete" index:self.deleteIndex section:self.deleteSection];
    }
    btn.selected = !btn.selected;
}
-(void)deleteBtnAction:(UIButton *)btn{
    [self.saveDelegate bringSaveOrDelete:self.bringModel type:@"delete" index:self.deleteIndex section:self.deleteSection];
}
-(void)awakeFromNib {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
